********************************************************************************
********************************************************************************

							* INFRASTRUCTURE SURVEY *

********************************************************************************
********************************************************************************


use "$Endline_dtRaw/facilities/Questionrio 5 - Instalaes.dta", clear

label drop _all

********************************************************************************

// Identification cleaning, apply to all rosters!

drop if inlist(interview__key,"26-80-93-00","07-37-23-21","66-89-16-38", ///
				"67-04-66-96","44-40-92-41","70-71-16-50","21-28-95-57", ///
				"17-59-61-75","50-78-73-40")

********************************************************************************

replace school_id = 29 if interview__key == "23-52-91-49"

sort school_id

********************************************************************************
********************************************************************************

* Survey solutions variables

order interview__key interview__id sssys_irnd has__errors

drop interview__status

label variable interview__id "Unique 32-character long identifier"
label variable interview__key "Interview key (XX-XX-XX-XX format)"
label variable sssys_irnd "Random number range 0..1 associated with interview"
label variable has__errors "Errors count in the interview"

rename interview__id 	ss_id
rename interview__key 	ss_key
rename sssys_irnd 		ss_random
rename has__errors 		ss_errors

* Identification variables

order 	school_id school_name supervisor interviewer 	///
		date_time_start date_time_end, after(ss_errors)

// absolutely no value in the gps data, only 5 observations

drop 	gps__Latitude gps__Longitude gps__Accuracy gps__Altitude 	///
		gps__Timestamp consensus

// add municipality and treatment variable to data set

gen municipality = .

replace municipality = 1 if inlist(school_id,1,2,3,4,5,6,7,8,9,10,12,13,	///
	14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,37,		///
	40,45,61,86,110)

replace municipality = 2 if inlist(school_id,23,43,44,46,47,48,49,50,51,	///
	52,53,54,55,56,57,58,59,60,62,63,64,65,66,67,68,69,70,71,72,73,75,76,	///
	77,78,79,80,81)

replace municipality = 3 if inlist(school_id,11,36,38,39,41,42,74,82,83,84,	///
	85,87,88,89,90,91,92,93,94,95,96,97,98,99,101,102,103,104,105)

replace municipality = 4 if inlist(school_id,118,119,120,121,122,123,124,125)
replace municipality = 5 if inlist(school_id,100,112,113,114,115,116,117)
replace municipality = 6 if inlist(school_id,109,111)
replace municipality = 7 if inlist(school_id,106,107)
replace municipality = 8 if inlist(school_id,126)
replace municipality = 9 if inlist(school_id,108)

gen treatment = .

replace treatment = 0 if inlist(school_id,3,4,7,10,16,18,20,23,26,29,33, ///
	39,42,53,55,62,63,64,65,78,81,84,87,88,89,92,98,108,109,116,117,121)

replace treatment = 1 if inlist(school_id,15,19,22,27,32,36,37,40,49,51, ///
	56,57,58,61,66,68,72,76,79,82,83,86,91,95,100,102,103,106,110,114,125,126)

replace treatment = 2 if inlist(school_id,1,5,6,11,12,14,21,24,25,31,41, ///
	48,50,52,59,70,73,77,80,90,93,94,101,105,107,111,112,113,119,120,124)

replace treatment = 3 if inlist(school_id,2,8,9,13,17,28,30,34,35,38,43, ///
	44,45,46,47,54,60,67,69,71,74,75,85,96,97,99,104,115,118,122,123)

********************************************************************************

// labelssss

#delimit ;
;
label define treatment
0	"Control group"
1	"Door-to-door"
2	"Meetings"
3	"Both treatments"
;
label define municipality
1	"Sumbe"
2	"Porto Amboim"
3	"Gabela"
4	"Conda"
5	"Ebo"
6	"Quilenda"
7	"Seles"
8	"Cassongue"
9	"Quibala"
;
label define enum
1	"Antonio Lopes Miguel Rocha"
2	"Augusta Júlio Miranda"
3	"César Zau Joaquim Morais Mendes"
4	"Edesdeth Abrantes Fonseca"
5	"Fernando Portugal Leonardo"
6	"Filipe Quissana Cariengue"
7	"Herlander Mauro Lemos Lima"
8	"Manuel Aguinaldo Barroso Moreira Francisco"
9	"Albertina de Oliveira Armando"
10	"Augusto Viana Kupinga"
11	"Beatriz Laurinda Teixeira da Costa"
12	"Carlos da Fonseca Ricardo "
13	"Carolina Germano da Silva"
14	"Felizarda Luis Avelino"
15	"Hermínia Caetano Kalolo"
16	"Ilídio Duarte Reis Cardoso"
17	"Isabel Marcolino Lisboa"
18	"João Monteiro Bimbe"
19	"José Alberto Nungulo Kamucunja"
20	"Manuel Pinheiro Inacio Ernesto"
21	"Marimilia Francisco Fernando"
22	"José Bambi Kandjeke"
23	"Samora Paulo José Ganga"
24	"Victória Maneco Tavares"
25	"Adelaide Fernando Miguens"
26	"Adelaide Marcelina da Cruz Zeferino"
27	"Adilson Fragoso Pascoal Inácio Filipe"
28	"Amaro Alberto Chipa"
29	"Ana Paula Silvestre Ventura"
30	"Antonio Calei Pedro da Silva"
31	"António Manuel da Silva"
32	"Bernardo Miguêns Gimbo"
33	"Bernardo Sungo Manuel Zau"
34	"Bráulio Eanes Gabriel Alberto"
35	"Bruno José Fernando Viegas"
36	"Carvalinho António Correia"
37	"Cirineu Jr. Jorge"
38	"Domingos Guimarães Sumano"
39	"Elvino António Sobral Salomão"
40	"Etelvina Albertina Francisco Teixeira"
41	"Helder Silvestre Lino Tomé Pinto"
42	"Joana Pinto Soma"
43	"Jonas Frederico Albino Rangel"
44	"José Catraio António"
45	"José Luciano Januário"
46	"José Maria Sumano Delgado"
47	"José Pinto Fançony"
48	"Magalhães Salissinga Gomes"
49	"Maria de Fátima Paulo Feliciano"
50	"Mariza Fernando Manuel Vales"
51	"Monteiro Bastos"
52	"Nilton Januário Valentim"
53	"Sara Claúdio Vidro Aguiar Maneco"
54	"Telma da Conceição Mweleyavo António"
55	"Teresa Aguiar da Costa"
56	"Teresa Kizua Kassucada"
57	"Bruno Avelino Segunda"
58	"Inocêncio Mário Cupenala"
59	"Sabino Jorge Bira"
;
label define resp_position
1 `"Director"'
2 `"Deputy director"'
3 `"Teacher"'
4 `"Member of school secretary"'
;
label define dummy
0	"No"
1 	"Yes"
;
label define I01_03
0 `"No water"'
1 `"Network connection"'
2 `"Public fountain"'
3 `"?Water catchment? (Captação)"'
4 `"Protected well"'
5 `"Protected spring"'
6 `"Rainwater harvesting"'
7 `"Water trucks"'
8	"Students bring from home"
9	"River"
;
label define I01_05
1 `"At school"'
2 `"Less than 100 meters"'
3 `"100 - 500 meters"'
4 `"500 meters - 1 km"'
5 `"1 - 5 km"'
6 `"More than 5 km"'
;
label define I01_24
1 "Yes, construction"
2 "Yes, improvement"
0 "No"
;
#delimit cr

********************************************************************************

label values supervisor interviewer enum
label values resp_position resp_position
label values municipality municipality
label values treatment treatment

label variable date_time_start `"Date and time start interview"'
label variable date_time_end `"Date and time end interview"'
label variable school_id `"ID number school"'
label variable municipality "Municipality"
label variable treatment "Treatment group"
label variable school_name `"Name school"'
label variable supervisor `"Supervisor"'
label variable interviewer `"Interviewer"'
label variable resp_name `"Name respondent"'
label variable resp_position `"Position of respondent at the school"'
label variable resp_tel `"Phone number main respondent"'

replace date_time_start = "" if date_time_start=="##N/A##"
replace date_time_end = "" if date_time_end=="##N/A##"
replace resp_name = "Sabalo Januario Fernando" if resp_name=="1"
replace resp_name = "Gonçalves Manuel João" if resp_name=="##N/A##"
replace resp_position = 4 if resp_position==8
replace resp_tel = . if resp_tel==999
replace interviewer = supervisor if interviewer== -999999999
drop resp_position_other

* General school infrastructures

foreach var in 	I01_01 I01_02 I01_03 I01_04 I01_05 I01_06 I01_07 I01_08 ///
				I01_09 I01_10 I01_11 I01_12 I01_13 I01_14 I01_15 I01_16	///
				I01_17 I01_18 I01_19 I01_20 I01_21 I01_22__1 I01_22__2 	///
				I01_22__3 I01_22__4 I01_22__5 I01_22__6 I01_22__7 		///
				I01_23 I01_24{
replace `var' = . if `var' == 999
replace `var' = . if `var' == -999999999
}

label values I01_01 dummy
label variable I01_01 `"Electricity available"'
label variable I01_02 `"Days without electricity per month"'

label values I01_03 I01_03
label variable I01_03 `"Water available and type"'
label variable I01_04 `"Days without water per month"'

replace I01_03 = . if I01_03==7

replace I01_03 = 2 if I01_03_other=="É transportado do chafariz  com baldes"
replace I01_03 = 2 if I01_03_other=="Manivela"
replace I01_03 = 7 if I01_03_other=="Através de camiões cisternas"
replace I01_03 = 7 if I01_03_other=="Compra por base de camiões sisternas"
replace I01_03 = 7 if I01_03_other=="Por via camiões sisternas"
replace I01_03 = 7 if I01_03_other=="Por via de camiões sisternas"
replace I01_03 = 7 if I01_03_other=="Por via de camiões sisternas; tanque protegido"
replace I01_03 = 8 if I01_03_other=="Alunos Trazem de casa"
replace I01_03 = 8 if I01_03_other=="Os alunos transportam com baldes e despejam no tanque"
replace I01_03 = 8 if I01_03_other=="Os alunos trazem água de suas casas em bidons"
replace I01_03 = 8 if I01_03_other=="Os alunos trazem em bidons de cinco litros do rio ou suas casas"
replace I01_03 = 9 if I01_03_other=="Buscar no rio"
replace I01_03 = 9 if I01_03_other=="Rio"

*replace I01_03 = 3 if I01_03_other=="Tamque"
*replace I01_03 = 3 if I01_03_other=="Tangque"

drop I01_03_other

label values I01_05 I01_05
label variable I01_05 `"Distance to water source"'


label values I01_06 dummy
label variable I01_06 `"Phone available"'

label values I01_07 dummy

label variable I01_07 `"PC available"'
label variable I01_08 `"Number of PCs (total)"'
label variable I01_09 `"Number of PCs (teachers)"'
label variable I01_10 `"Number of PCs (students)"'
label variable I01_11 `"Number of PCs (internet)"'
label variable I01_12 `"Number of school books"'
label variable I01_13 `"Number of exercise books"'
label variable I01_14 `"Number of registration books"'
label variable I01_15 `"Number of boxes of chalk"'
label variable I01_16 `"Number of educative posters"'

label values I01_17 dummy

label variable I01_17 `"Security personel"'
label variable I01_18 `"Number of days security personel is present"'

label values I01_19 dummy
label variable I01_19 `"Cleaning personel"'
label variable I01_20 `"Number of days cleaning personel is present"'

label values 	I01_21 I01_22__1 I01_22__2 I01_22__3 I01_22__4 I01_22__5 	///
				I01_22__6 I01_22__7 dummy

label variable I01_21 `"Fence/wall around school"'

label variable I01_22__1 `"material used for fence / wall:Tijolo/cimento"'
label variable I01_22__2 `"material used for fence / wall:Arame farpado"'
label variable I01_22__3 `"material used for fence / wall:Adobe"'
label variable I01_22__4 `"material used for fence / wall:Grade de ferro"'
label variable I01_22__5 `"material used for fence / wall:Chapa"'
label variable I01_22__6 `"material used for fence / wall:Madeira"'
label variable I01_22__7 `"material used for fence / wall:Outro (especifique)"'

drop I01_22_other

label values I01_23 dummy
label variable I01_23 `"The fence / wall can be closed and locked"'

label values I01_24 I01_24
label variable I01_24 "Improvement or construction of fence during last two years"

label variable types__1 `"Classrooms"'
label variable types__2 `"Director/teacher/staff rooms"'
label variable types__3 `"Bathrooms"'
label variable types__4 `"Garden/outdoor space"'
label variable types__5 `"Student dorms"'
label variable types__6 `"Teacher/staff housing"'
label variable types__7 `"Library"'
label variable types__8 `"Storage"'
label variable types__9 `"Other spaces"'

lab val types__1 types__2 types__3 types__4 types__5 types__6 	///
		types__7 types__8 types__9 dummy

********************************************************************************

save "$Endline_dtRaw/_intermediate files/facilities.dta", replace

********************************************************************************

* add other respondents

use "$Endline_dtRaw/facilities/I02_roster.dta", clear

label drop _all

********************************************************************************

// Identification cleaning, apply to all rosters!

drop if inlist(interview__key,"26-80-93-00","07-37-23-21","66-89-16-38", ///
				"67-04-66-96","44-40-92-41","70-71-16-50","21-28-95-57", ///
				"17-59-61-75","50-78-73-40")

********************************************************************************


drop if I02_01==-999999999

rename interview__id 	ss_id
rename interview__key 	ss_key
rename I02_roster__id	roster_id

order ss_id ss_key roster_id

label variable ss_id "Unique 32-character long identifier"
label variable ss_key "Interview key (XX-XX-XX-XX format)"
label variable roster_id `"Roster instance identifier"'

sort ss_id roster_id

drop ao ae space I02_07__999 I02_08__999 I02_09__999 I02_10__999

********************************************************************************


foreach var in 	I02_01 I02_02 I02_03 I02_04 I02_05 I02_06 I02_14	///
				I02_15 I02_16 I02_17 I02_18 I02_19 I02_33 I02_34 	///
				I02_35 I02_20 I02_21 I02_22 I02_23 I02_24 I02_25 	///
				I02_26 I02_27 I02_28 I02_29 I02_30 I02_31{
replace `var' = . if `var' == 999
replace `var' = . if `var' == -999999999
}

********************************************************************************

// labelssss

#delimit ;
;
label define dummy
0	"No"
1 	"Yes"
;
label define I02_01
1 `"Classroom"'
2 `"Director/teacher/staff room"'
3 `"Bathroom"'
4 `"Garden/outdoor space"'
5 `"Student dorm"'
6 `"Teacher/staff house"'
7 `"Library"'
8 `"Storage"'
9 `"Computer science room"'
10 "Multipurpose hall"
;
label define I02_02
1 `"Improvised (termporary use: church, house etc.)"'
2 `"Provisional (sticks, clay, galvanized plates)"'
3 `"Final (cement and bricks)"'
;
label define I02_03
1 `"Clay (terra batida)"'
2 `"Sand"'
3 `"Cement"'
4 `"Outro (especifique)"'
;
label define I02_04
1 `"Good state"'
2 `"Bad state but can be used"'
3 `"Bad state and cannot be used"'
;
label define I02_06
1 "Yes, construction"
2 "Yes, improvement"
0 "No"
;
label define I02_24
0 `"Not connected to public sewage"'
1 `"Connected to public sewage"'
2 `"Connected to septic tank"'
;
#delimit cr

********************************************************************************

label values I02_01 I02_01
label variable I02_01 `"Type of space"'

replace I02_01 = . if I02_01 == 9
replace I02_01 = 9 if I02_01_other=="sala de informática"
replace I02_01 = 10 if I02_01_other=="Pavilhão multi uso"

drop I02_01_other

label values I02_02 I02_02
label variable I02_02 `"Type of construction"'

replace I02_02 = 2 if I02_02_other=="Alumínio"
replace I02_02 = 2 if I02_02_other=="chapa de zinco"
replace I02_02 = 2 if I02_02_other=="Chapa de zinco"
replace I02_02 = 2 if I02_02_other=="chapas"
replace I02_02 = 2 if I02_02_other=="Chapas"

drop if ss_key=="75-42-30-23" & inlist(roster_id,9,10)
drop if ss_key=="98-35-73-45" & roster_id==5

replace I02_01=4 if ss_key=="98-35-73-45" & roster_id==9

replace I02_02=. if ss_key=="98-35-73-45" & roster_id==9

replace I02_05=0 if ss_key=="98-35-73-45" & roster_id==9

replace I02_10__1=0 if ss_key=="98-35-73-45" & roster_id==9
replace I02_10__2=0 if ss_key=="98-35-73-45" & roster_id==9
replace I02_10__3=0 if ss_key=="98-35-73-45" & roster_id==9
replace I02_10__4=1 if ss_key=="98-35-73-45" & roster_id==9
replace I02_10__5=0 if ss_key=="98-35-73-45" & roster_id==9

replace I02_26=. if ss_key=="98-35-73-45" & roster_id==9
replace I02_27=. if ss_key=="98-35-73-45" & roster_id==9

drop I02_02_other


label values I02_03 I02_03
label variable I02_03 `"Type of ground"'

replace I02_03 = . if I02_03==4
replace I02_03 = 1 if I02_03_other=="Cimento e terra batida"

drop I02_03_other

label values I02_04 I02_04
label variable I02_04 `"State of space"'

label values I02_05 dummy
label variable I02_05 `"Space can always be used"'

label values I02_06 I02_06
label variable I02_06 `"Improvements to space"'


label variable I02_07__0 `"Classroom used by pre-school classes"'
label variable I02_07__1 `"Classroom used by 1st grade classes"'
label variable I02_07__2 `"Classroom used by 2nd grade classes"'
label variable I02_07__3 `"Classroom used by 3rd grade classes"'
label variable I02_07__4 `"Classroom used by 4th grade classes"'
label variable I02_07__5 `"Classroom used by 5th grade classes"'
label variable I02_07__6 `"Classroom used by 6th grade classes"'
label variable I02_07__7 `"Classroom used by 7th grade classes"'
label variable I02_07__8 `"Classroom used by 8th grade classes"'
label variable I02_07__9 `"Classroom used by 9th grade classes"'
label variable I02_07__10 `"Classroom used by module 1 (grades 1 and 2)"'
label variable I02_07__11 `"Classroom used by module 2 (grades 3 and 4)"'
label variable I02_07__12 `"Classroom used by module 3 (grades 5 and 6)"'

lab val I02_07__0 I02_07__1 I02_07__2 I02_07__3 I02_07__4 I02_07__5  	///
		I02_07__6 I02_07__7 I02_07__8 I02_07__9 I02_07__10 I02_07__11 	///
		I02_07__12 dummy

gen I02_08__7 = 0 if I02_08__1!=.
replace I02_08__7 = 1 if I02_08_other=="Centro de Recurso Projecto do PAT"
replace I02_08__6 = 0 if I02_08_other=="Centro de Recurso Projecto do PAT"

label variable I02_08__1 `"Director uses this space"'
label variable I02_08__2 `"Deputy director uses this space"'
label variable I02_08__3 `"Teachers use this space"'
label variable I02_08__4 `"Secretary staff use this space"'
label variable I02_08__5 `"Parent committee uses this space"'
label variable I02_08__6 `"Nobody uses the space, just storage"'
label variable I02_08__7 `"Used by others (not school related)"'

drop I02_08_other

order I02_08__7, after(I02_08__6)

lab val I02_08__1 I02_08__2 I02_08__3 I02_08__4 I02_08__5 I02_08__6		///
		I02_08__7 dummy


label variable I02_09__0 `"Everyone can use this bathroom"'
label variable I02_09__1 `"Boys can use this bathroom"'
label variable I02_09__2 `"Girls can use this bathroom"'
label variable I02_09__3 `"Director can use this bathroom"'
label variable I02_09__4 `"Adult men can use this bathroom"'
label variable I02_09__5 `"Adult women can use this bathroom"'

replace I02_09__0 = 1 if I02_09__1 == 1 & I02_09__2 == 1 & I02_09__3 == 1 	///
						& I02_09__4 == 1 & I02_09__5 == 1

foreach var in I02_09__1 I02_09__2 I02_09__3 I02_09__4 I02_09__5{
replace `var' = 0 if I02_09__0 == 1
}

lab val I02_09__0 I02_09__1 I02_09__2 I02_09__3 I02_09__4 I02_09__5 dummy

replace I02_10__1 = 1 if I02_10_other == "embelezamento da escola"
replace I02_10__1 = 1 if I02_10_other == "Recreação"
replace I02_10__2 = 1 if I02_10_other == "Aulas de Educação Física"
replace I02_10__2 = 1 if I02_10_other == "Aulas de educação física"
replace I02_10__2 = 1 if I02_10_other == "Prática de Educação Física"


drop I02_10__5
gen I02_10__5 = 0 if I02_10__1!=.
replace I02_10__5 = 1 if I02_10_other == "Actividades extracurriculares"

gen I02_10__6 = 0 if I02_10__1!=.
replace I02_10__6 = 1 if I02_10_other == "Informatizar documentos"

label variable I02_10__1 `"Outdoor space not used"'
label variable I02_10__2 `"Ourdoor space used for sports"'
label variable I02_10__3 `"Ourdoor space used for plants"'
label variable I02_10__4 `"Ourdoor space used to give classes"'
label variable I02_10__5 `"Outdoor space used for extracurricular activities"'
label variable I02_10__6 `"Outdoor space used to publish information"'

lab val I02_10__1 I02_10__2 I02_10__3 I02_10__4 I02_10__5 I02_10__6 dummy

drop I02_10_other

order I02_10__5 I02_10__6, after(I02_10__4)

drop I02_11

label variable I02_12 `"Number of books in this space"'

replace I02_12 = . if I02_12==0

replace I02_13__4 = 1 if I02_13_other=="Arquivos mortos e casa dos guardas"
drop I02_13__6 I02_13_other

label variable I02_13__1 `"Storage for teaching material of teachers"'
label variable I02_13__2 `"Storage for teaching material of student"'
label variable I02_13__3 `"Storage for sport equipment"'
label variable I02_13__4 `"Storage for administration and archives"'
label variable I02_13__5 `"Storage for furniture"'

lab val I02_13__1 I02_13__2 I02_13__3 I02_13__4 I02_13__5 dummy


label variable I02_14 `"Number of chair in this space"'
label variable I02_15 `"Number of broken chairs in this space"'
label variable I02_16 `"Number of new chairs in this space"'
label variable I02_17 `"Number of desks and tables in this space"'
label variable I02_18 `"Number of broken desks and tables in this space"'
label variable I02_19 `"Number of new desks and tables in this space"'
label variable I02_33 `"Number of chair and desk sets in this space"'
label variable I02_34 `"Number of broken chairs and desk sets in this space"'
label variable I02_35 `"Number of new chair and desk sets in this space"'
label variable I02_20 `"Number of chalk boards in this space"'
label variable I02_21 `"Number of new chalk boards in this space"'


lab val I02_22 I02_23 I02_25 I02_26 I02_27 dummy

label variable I02_22 `"There is light/electricity in this space"'
label variable I02_23 `"There is running water in this space"'
label variable I02_24 `"Type of bathroom"'
label variable I02_25 `"Bathroom has an automatic flush"'
label variable I02_26 `"Space has door"'
label variable I02_27 `"Space can be locked"'

lab val I02_24 I02_24
replace I02_24 = . if I02_24==3

label variable I02_28 `"Number of windows"'
label variable I02_29 `"Number of glass windows"'
label variable I02_30 `"Number of new windows in this space"'
label variable I02_31 `"Days since last cleaning of this space"'

label variable I02_32_01 `"Length space"'
label variable I02_32_02 `"Width space"'

********************************************************************************

save "$Endline_dtRaw/_intermediate files/facilities_I02.dta", replace

********************************************************************************

use "$Endline_dtRaw/_intermediate files/facilities.dta", clear

merge 1:m ss_key using "$Endline_dtRaw/_intermediate files/facilities_I02.dta"

********************************************************************************





// Keep useful facilities data and make sure it can be merged meaningfully





********************************************************************************

drop ss_key ss_id ss_random ss_errors municipality treatment roster_id

sort school_id

// Section 1

// There is electricity if at least 20 days per month available

gen electricity = I01_01
replace electricity = 0 if I01_02>10

lab var electricity "At least 20 days per month electricity"

// There is water if available at school and at least 20 days per month available

gen water = I01_03
replace water = 1 if inlist(water,2,3,4,5,7)
replace water = 0 if water==8
replace water = 0 if I01_04>10

lab var water "At least 20 days per month water at the school"

// Materials

rename I01_12 books
rename I01_13 books_ex
rename I01_14 books_reg
rename I01_15 chalk_box
rename I01_16 poster

replace books = books/1000 if books>7000
replace books_ex = books_ex/1000 if books>7000
replace books_reg = . if books_reg==507

// Security if at least 5 days

rename I01_17 security
replace security = 0 if I01_18<5

// Cleaning staff if at least 5 days (only 4 schools with 2 or 3 days

rename I01_19 cleaner
replace cleaner = 0 if I01_20<5

// Fence

rename I01_21 fence
gen fence_bricks = 0 if fence==1
replace fence_bricks = 1 if I01_22__1==1

gen fence_barbedwire = 0 if fence==1
replace fence_barbedwire = 1 if I01_22__2==1

lab var fence_bricks "Fence of bricks"
lab var fence_barbedwire "Fence with barbed wire"

rename I01_23 fence_lock

gen fence_improvement = I01_24 if fence==1
recode fence_improvement (1=0) (2=1)

gen fence_construction = I01_24 if fence==1
recode fence_construction (2=0)

gen fence_impr_const = I01_24 if fence==1
recode fence_impr_const (2=1)

lab var fence_improvement 	"Fence was improved during last 3 years"
lab var fence_construction 	"Fence was constructed during last 3 years"
lab var fence_impr_const	"Fence was improved or constructed during last 3 years"

foreach var in fence_bricks fence_barbedwire fence_lock fence_improvement ///
	fence_construction fence_impr_const{
replace `var' = 0 if `var'==.
}

// Control variables

rename I01_05 water_distance
rename I01_06 phone
rename I01_07 pc


*outcome variables
order 	electricity water books books_ex books_reg chalk_box poster security ///
		cleaner fence fence_bricks fence_barbedwire fence_lock 				///
		fence_improvement fence_construction fence_impr_const				///
		, after(resp_tel)

*control variables
order water_distance phone pc, after(fence_impr_const)

// Number of spaces per school

sort school_id

by school_id: egen spaces = count(school_id)
lab var spaces "Total number of spaces"


egen classrooms_dummy = anymatch(I02_01), v(1)
by school_id: egen classrooms = total(classrooms_dummy)
lab var classrooms "Total number of classrooms"

egen teacherrooms_dummy = anymatch(I02_01), v(2)
by school_id: egen teacherrooms = total(teacherrooms_dummy)
lab var teacherrooms "Total number of teacher rooms"

egen bathrooms_dummy = anymatch(I02_01), v(3)
by school_id: egen bathrooms = total(bathrooms_dummy)
lab var bathrooms "Total number of bathrooms"

egen classrooms_final_d = anymatch(I02_02) if I02_01==1, v(3)
by school_id: egen classrooms_final = total(classrooms_final_d)
lab var classrooms_final "Total number of classrooms with final construction"

gen classrooms_final_share = classrooms_final/classrooms
lab var classrooms_final_share "Share classrooms with final construction"

egen classrooms_improvement_d = anymatch(I02_06) if I02_01==1, v(1,2)
by school_id: egen classrooms_improvement = total(classrooms_improvement_d)
lab var classrooms_improvement "Total number of classrooms that were improved"

gen classrooms_improvement_share = classrooms_improvement/classrooms
lab var classrooms_improvement_share "Share of classrooms that were improved"

by school_id: egen classrooms_cleaned = mean(I02_31) if I02_01==1
lab var classrooms_cleaned "Average days since last cleaning classroom"


egen bathrooms_improvement_d = anymatch(I02_06) if I02_01==3, v(1,2)
by school_id: egen bathrooms_improvement = total(bathrooms_improvement_d)
lab var bathrooms_improvement "Total number of bathrooms that were improved or new"

gen bathrooms_improvement_share = bathrooms_improvement/bathrooms
lab var bathrooms_improvement_share "Share of bathrooms that were improved or new"

by school_id: egen bathrooms_cleaned = mean(I02_31) if I02_01==3
lab var bathrooms_cleaned "Average days since last cleaning bathroom"

gen bathrooms_students_d = 1 if I02_09__0==1 | I02_09__1==1 | I02_09__2==1
by school_id: egen bathrooms_students = total(bathrooms_students_d)

by school_id: egen chairs = total(I02_14) if I02_01==1
by school_id: egen chairs_broken = total(I02_15) if I02_01==1
by school_id: egen chairs_new = total(I02_16) if I02_01==1

gen chairs_per_classroom = chairs/classrooms
gen chairs_new_per_classroom = chairs_new/classrooms

by school_id: egen desks = total(I02_17) if I02_01==1
by school_id: egen desks_broken = total(I02_18) if I02_01==1
by school_id: egen desks_new = total(I02_19) if I02_01==1
gen desks_per_classroom = desks/classrooms
gen desks_new_per_classroom = desks_new/classrooms
by school_id: egen chair_desk = total(I02_33) if I02_01==1
by school_id: egen chair_desk_broken = total(I02_34) if I02_01==1
by school_id: egen chair_desk_new = total(I02_35) if I02_01==1
gen chair_desk_per_classroom = chair_desk/classrooms
gen chair_desk_new_per_classroom = chair_desk_new/classrooms
by school_id: egen chalkboard = total(I02_20) if I02_01==1
by school_id: egen chalkboard_new = total(I02_21) if I02_01==1
gen chalkboard_per_classroom = chalkboard/classrooms
gen chalkboard_new_per_classroom = chalkboard_new/classrooms

foreach var in classrooms_cleaned bathrooms_cleaned chairs chairs_broken 	///
	chairs_new chairs_per_classroom chairs_new_per_classroom desks 			///
	desks_broken desks_new desks_per_classroom desks_new_per_classroom 		///
	chair_desk chair_desk_broken chair_desk_new chair_desk_per_classroom 	///
	chair_desk_new_per_classroom chalkboard chalkboard_new 					///
	chalkboard_per_classroom chalkboard_new_per_classroom{
	by school_id: egen x_`var' = max(`var')
	replace `var' = x_`var'
	drop x_`var'
	}


*outcome variables
order spaces classrooms classrooms_final classrooms_final_share 			///
	classrooms_improvement classrooms_improvement_share classrooms_cleaned teacherrooms 		///
	bathrooms bathrooms_improvement bathrooms_improvement_share bathrooms_cleaned bathrooms_students 	///
	chairs chairs_broken chairs_new chairs_per_classroom 					///
	chairs_new_per_classroom desks desks_broken desks_new 					///
	desks_per_classroom desks_new_per_classroom chair_desk chair_desk_broken ///
	chair_desk_new chair_desk_per_classroom chair_desk_new_per_classroom 	///
	chalkboard chalkboard_new chalkboard_per_classroom 						///
	chalkboard_new_per_classroom, after(fence_impr_const)

keep school_id school_name supervisor interviewer date_time_start date_time_end resp_name resp_position resp_tel electricity water books books_ex books_reg chalk_box poster security cleaner fence fence_bricks fence_barbedwire fence_lock fence_improvement fence_construction fence_impr_const spaces classrooms classrooms_final classrooms_final_share classrooms_improvement classrooms_improvement_share classrooms_cleaned teacherrooms bathrooms bathrooms_improvement bathrooms_improvement_share bathrooms_cleaned bathrooms_students chairs chairs_broken chairs_new chairs_per_classroom chairs_new_per_classroom desks desks_broken desks_new desks_per_classroom desks_new_per_classroom chair_desk chair_desk_broken chair_desk_new chair_desk_per_classroom chair_desk_new_per_classroom chalkboard chalkboard_new chalkboard_per_classroom chalkboard_new_per_classroom water_distance phone pc

duplicates drop

rename * I_*

rename I_school_id school_id

save "$Endline_dtRaw/_intermediate files/facilities.dta", replace
